
function [tau0Vec , x0Mtx] = Bcr4bp_RandLI3D(coord , orbNum , dvBound , aux)
%
% 随机生成LI状态，3D
%
% 例：
% Bcr4bp_RandLI3D('emrot' , 500 , [0.65 , 0.65] , aux);
% 
% 20220926
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tau0Vec = zeros(orbNum , 1);
x0Mtx = zeros(orbNum , 6);

% LLO (km)
lloRmag = 1738 + 100;
muMoon = aux.dim.mu_M;

if strcmp(coord , 'emrot')

    % 循环orbNum
    for iLoop = 1 : orbNum

        % 随机相位角和脉冲
        alpha = unifrnd(0 , 2 * pi);
        dv = unifrnd(dvBound(1) , dvBound(2));
        thetas = unifrnd(0 , 2 * pi);
        tau0 = thetas / aux.EMRot.ws;

        % 计算惯性系TLI状态
        rMag = lloRmag / aux.dim.EMRot_l;
        vMag = (sqrt(muMoon / lloRmag) + dv) / aux.dim.EMRot_v;
        xx_ec_ini =  [rMag * cos(alpha); rMag * sin(alpha); 0 ; -vMag * sin(alpha); vMag * cos(alpha) ; 0];

        % 计算旋转系TLI状态
        xx_b1c_rot = Bcr4bp_Iner2Rot3D(tau0 , xx_ec_ini , 'p2' , 'emrot' , aux);

        tau0Vec(iLoop , :) = tau0;
        x0Mtx(iLoop , :) = xx_b1c_rot';

    end

elseif strcmp(coord , 'sb1rot')

    % 循环orbNum
    for iLoop = 1 : orbNum

        % 随机相位角和脉冲
        alpha = unifrnd(0 , 2 * pi);
        dv = unifrnd(dvBound(1) , dvBound(2));
        thetam = unifrnd(0 , 2 * pi);
        tau0 = thetam / aux.SB1Rot.wm;

        % 计算惯性系TLI状态
        rMag = lloRmag / aux.dim.SB1Rot_l;
        vMag = (sqrt(muMoon / lloRmag) + dv) / aux.dim.SB1Rot_v;
        xx_ec_ini =  [rMag * cos(alpha); rMag * sin(alpha); 0 ; -vMag * sin(alpha); vMag * cos(alpha) ; 0];

        % 计算旋转系TLI状态
        xx_b2c_rot = Bcr4bp_Iner2Rot3D(tau0 , xx_ec_ini , 'p2' , 'sb1rot' , aux);

        tau0Vec(iLoop , :) = tau0;
        x0Mtx(iLoop , :) = xx_b2c_rot';

    end

else

    fprintf('wrong!')
    return;

end

end
